package com.atguigu.config;

import com.atguigu.Interceptor.MyInterceptor;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@EnableWebMvc //相当于给 handelerAdapter 配置了json转换器 <mvc:annotation-driven/>
@Configuration
@ComponentScan(basePackages = "com.atguigu")
public class MvcConfig implements WebMvcConfigurer {

//    @Bean
//    public RequestMappingHandlerMapping handerMapper()
//    {
//        return new RequestMappingHandlerMapping();
//    }
//    @Bean
//    public RequestMappingHandlerAdapter handlerAdapter()
//    {
//        return new RequestMappingHandlerAdapter();
//    }

    @Override
    public void addInterceptors(InterceptorRegistry registry){

        //配置方案1：拦截全部请求
        //registry.addInterceptor(new MyInterceptor());

        //配置方案2：拦截指定请求 .addPathPatterns("/user/data1") 支持模糊
        //registry.addInterceptor(new MyInterceptor()).addPathPatterns("/user/data1");

        //配置方案3：排除拦截指定请求 .excludePathPatterns("/user/data2");
        registry.addInterceptor(new MyInterceptor())
                .addPathPatterns("/user/**")
                .excludePathPatterns("/user/data2");
    }

}
